// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using NUnit.Framework;
using System;
using System.Text;
using SoapTrace.Core.Communication;
using Microsoft.VisualBasic;

namespace SoapTrace.Tests
{
	/// <summary>
	/// Test fixture to test the parsing of a "chunked" soap response which has two
	/// chunked parts.  See <see cref="ChunkedHttpResponseSingleChunkTextFixture"/> for 
	/// an overview of Http chunking.
	/// </summary>
	[ TestFixture ]
	public class ChunkedHttpResponseMultipleChunksTestFixture
	{
		/// <summary>
		/// The complete Http response message to be parsed.
		/// </summary>
		private string m_HttpResponseMessage = String.Empty;

		/// <summary>
		/// The Http header.
		/// </summary>
		private string m_HttpHeader = "HTTP/1.1 200 OK\r\nDate: Wed, 07 Jul 2004 18:26:27 GMT\r\nServer: WebLogic Server 8.1 SP2 Fri Dec 5 15:01:51 PST 2003 316284 WebLogic Server 8.1 SP2 Fri Dec 5 15:01:51 PST 2003 316284 WebLogic Server 8.1 SP2 Fri Dec 5 15:01:51 PST 2003 316284 \r\nContent-Type: text/xml\r\nTransfer-Encoding: chunked\r\n\r\n";																																																																					   
		
		/// <summary>
		/// The expected message body of the http response that we
		/// should get after the complete message is parsed by the 
		/// <see cref="SoapTrace.Core.Communication.SoapResponse"/> object.
		/// </summary>
		private string m_HttpMessageBody;

		/// <summary>
		/// The soap response object under test.
		/// </summary>
		private SoapResponse m_Response = new SoapResponse( );

		/// <summary>
		/// Initialises each test.  This creates a chunked http message containing two
		/// chunks of "Hello" and "World".  
		/// </summary>
		[ SetUp ]
		public void Init( )
		{
			// Create chunked http message body.

			string[ ] MessageChunks = new string[ ] { "Hello", "World" };

			// The http message body including chunked data.
			
			string ChunkedHttpMessageBody = String.Empty;
			
			m_HttpMessageBody = String.Empty;

			foreach( string Message in MessageChunks )
			{
				m_HttpMessageBody += Message;

				byte[ ] MessageBytes = UnicodeEncoding.UTF8.GetBytes( Message );

				string BytesSizeInHex = Conversion.Hex( MessageBytes.Length );
				ChunkedHttpMessageBody += String.Concat( BytesSizeInHex, "\r\n" );
				ChunkedHttpMessageBody += String.Concat( Message, "\r\n" );
			}

			ChunkedHttpMessageBody += "0\r\n\r\n";

			// Create complete http message.

			m_HttpResponseMessage = String.Concat( m_HttpHeader, 
				ChunkedHttpMessageBody );

			// Parse the complete message.

			byte[ ] Bytes = UnicodeEncoding.UTF8.GetBytes( m_HttpResponseMessage );

			for( int i = 0; i < Bytes.Length; ++i )
			{
				byte MessageByte = Bytes[ i ];

				bool EndOfMessage = m_Response.AddByte( MessageByte );

				if( Bytes.Length == ( i + 2 ) )
				{
					Assert.IsTrue( true );
				}
				
				if( Bytes.Length == ( i + 1 ) )
				{
					Assert.IsTrue( EndOfMessage, "Should be at the end of the message." );
				}
				else
				{
					Assert.IsFalse( EndOfMessage, "Should not be at the end of the message." );
				}
			}
		}

		/// <summary>
		/// Tests that a "chunked" soap response with multiple chunks is correctly parsed.
		/// </summary>
		[ Test ]
		public void MessageReceived( )
		{
			Assert.AreEqual( m_HttpHeader, m_Response.Header, "Soap Header is wrong." );
			Assert.AreEqual( m_HttpMessageBody, m_Response.Message, "Soap Message is wrong." );
		}
	}
}
